﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace FYPHP.Styles
{
    public partial class productupload : System.Web.UI.Page
    {        
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                conn.Open();
                SqlCommand cmdSearch = new SqlCommand("SELECT cat_name FROM Category WHERE cat_status='publish' ORDER BY cat_name", conn);
                SqlDataReader dtrSearch = cmdSearch.ExecuteReader();
                ddlCat.DataSource = dtrSearch;
                ddlCat.DataTextField = "cat_name";
                ddlCat.DataBind();
                dtrSearch.Close();
                conn.Close();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdInsert, cmdSearch;
                conn.Open();

                cmdSearch = new SqlCommand("SELECT cat_id FROM Category WHERE cat_name='" + ddlCat.SelectedValue + "'", conn);
                String catid = cmdSearch.ExecuteScalar().ToString();

                cmdInsert = new SqlCommand("INSERT INTO Products VALUES (@name, @desc, @quan, @price, @pic, @cat, @uid, @pviews, @pstatus, @pdate)", conn);
                cmdInsert.Parameters.AddWithValue("@name", txtPname.Text);
                cmdInsert.Parameters.AddWithValue("@desc", txtPdesc.Text);
                cmdInsert.Parameters.AddWithValue("@quan", txtStock.Text);
                cmdInsert.Parameters.AddWithValue("@price", txtPrice.Text);

                string uploadFolder = Request.PhysicalApplicationPath + "/uploads/products/";
                string now = String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now);
                string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                FileUpload1.SaveAs(uploadFolder + now + extension);
                cmdInsert.Parameters.AddWithValue("@pic", now + extension);

                cmdInsert.Parameters.AddWithValue("@cat", catid);
                cmdInsert.Parameters.AddWithValue("@uid", Session["ID"]);
                cmdInsert.Parameters.AddWithValue("@pviews", "0");
                cmdInsert.Parameters.AddWithValue("@pstatus", "Approve");
                cmdInsert.Parameters.AddWithValue("@pdate", DateTime.Now);

                int success = cmdInsert.ExecuteNonQuery();

                if (success == 1)
                {
                    MessageBox("Upload Successfully!");
                    Redirect("ProductsList.aspx");
                }

                conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

    }
}